const { decrypt, encrypt, createKey } = require('../core/util/util') //加密解密
const fs = require('fs').promises   //promisefs 异步
const fsSync = require('fs')        //fs   同步
const { pubKeyPath, priKeyPath } = require('../config')
module.exports = {
    getPublicKeySync() {    //同步获取公钥
        return fsSync.readFileSync(pubKeyPath, 'utf8')
    },
    async getPublicKey() {  //异步获取公钥   如果没有创建密匙对
        let key
        try {
            key = await fs.readFile(pubKeyPath, 'utf8')
        } catch (err) {
            createKey()
            key = await fs.readFile(pubKeyPath, 'utf8')
        }
        return key
    },
    async getPrivateKey() { //异步获取私钥  如果没有创建密匙对
        let key
        try {
            key = await fs.readFile(priKeyPath, 'utf8')
        } catch (err) {
            createKey()
            key = await fs.readFile(priKeyPath, 'utf8')
        }
        return key
    }
}